﻿using System;
using System.Web.UI.WebControls;
using System.Data;
using System.Web;

using VIT.Web.UI;
using VIT.Web.Controls;
using VIT.Provider;
using VIT.Library;
using VIT.Library.Web;

namespace VIT.Web.Admin
{
    public partial class CategoryList : VITComponent
    {
        Provider.Category obj;
        public string genOption = string.Empty;
        protected void Page_Load(object sender, EventArgs e)
        {
            obj = new Provider.Category();            
            
            if (!Page.IsPostBack)
            {
                loadParent();
                loadLang();                
            }
            pnAlert.Visible = false;
        }

        #region Methods
        [System.ComponentModel.DataObjectMethod(System.ComponentModel.DataObjectMethodType.Select)]
        public DataTable GetData(string SortColumn, string selectedId)
        {
            Provider.Category c = new Provider.Category();
            c.LanguageId = Session["language"].ToString();
            if(selectedId == null) selectedId = "0";            
            DataTable dt = c.GetAll();
            if (selectedId != "0")
            {
                string categorylist = dt.GetChildId(selectedId);                
                return c.GetAll(categorylist).SortTable(selectedId, columnChange: "Title", distance: "&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;", columnOrder: SortColumn);
            }
            else
                return dt.SortTable("0", columnChange: "Title", distance: "&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;", columnOrder: SortColumn);
        }

        private void Reload()
        {
            loadParent();
            ListView.DataBind();
        }

        private void loadParent()
        {
            obj.LanguageId = Session["language"].ToString();
            string selectedId = Request["category_parentid"];
            this.genOption = obj.GetAll().GenTagOption("Id", "Title", "0", selected: selectedId);
        }

        private void loadLang()
        {
            ddlLang.DataSource = new Language().getAll();
            ddlLang.DataTextField = "Name";
            ddlLang.DataValueField = "Id";
            ddlLang.DataBind();

            ddlLang.SelectedValue = Session["language"].ToString();
        }

        public void deleteList(string[] listId)
        {
            foreach (string id in listId)
            {
                if (obj.checkForDel(int.Parse(id))) obj.Delete(1, int.Parse(id)); // kiem tra co nhom con hay bai viet ko
                else
                {
                    pnAlert.Visible = true;
                    lblEmpty.Visible = true;
                }
            }
        }

        public void PublishList(int pub, string[] listId)
        {
            foreach (string id in listId)
                obj.Publish(pub, int.Parse(id));
        }

        #endregion

        #region Events

        protected void Delete_Click(object sender, EventArgs e)
        {
            string[] check = Request["itemCheckbox"].Split(',');
            deleteList(check);
            ListView.DataBind();
            loadParent();
        }

        protected void Insert_Click(object sender, EventArgs e)
        {
            RedirectComponent("Category", "CategoryEdit");
        }

        protected void Published_Click(object sender, EventArgs e)
        {
            string[] check = Request["itemCheckbox"].Split(',');
            PublishList(1, check);
            Reload();
        }

        protected void UnPublished_Click(object sender, EventArgs e)
        {
            string[] check = Request["itemCheckbox"].Split(',');
            PublishList(0, check);
            Reload();
        }

        protected void changePublish_Click(object sender, EventArgs e)
        {
            LinkSubmit btn = (LinkSubmit)sender;
            string check = btn.Param.ToString();

            string[] id = check.Split('|');
            int pub = (id[1] == "True") ? 0 : 1;
            obj.Publish(pub, int.Parse(id[0]));
            Reload();
        }

        protected void updateOrder_Click(object sender, EventArgs e)
        {
            LinkSubmit btn = (LinkSubmit)sender;
            string check = btn.Param.ToString();

            string[] id = check.Split('|');
            obj.Order(id[1], int.Parse(id[0]));
            Reload();
        }

        protected void Filter_Click(object sender, EventArgs e)
        {
            Reload();
        }

        protected void odsProducts_Selecting(object sender, ObjectDataSourceSelectingEventArgs e)
        {
            e.InputParameters["selectedId"] = Request["category_parentid"];
        }  

        protected void ddlLang_SelectedIndexChanged(object sender, EventArgs e)
        {
            Session["language"] = ddlLang.SelectedValue;
            Reload();
        }

        #endregion
        
    }
}
